<html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script src="./js/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
	//var wsUri = "ws://deanx.cn:7102/websocket";
	var wsUri = "ws://localhost:7102/websocket";
	var output;
	function init() {
		output = document.getElementById("output");
		testWebSocket();
		//setInterval("timer()", 1000);
	}
	function testWebSocket() {

		websocket.onopen = function(evt) {
			onOpen(evt)
		};
		websocket.onclose = function(evt) {
			//alert("close?");
			onClose(evt)
		};
		websocket.onmessage = function(evt) {
			onMessage(evt)
		};
		websocket.onerror = function(evt) {
			onError(evt)
		};
	}

	function onOpen(evt) {

		writeToScreen("CONNECTED");
		debugger;
		var data = {
			key : "abc",
			value : "qwerqwr",
		};
		var a = new Array();
		a.push(data);

		doSend(JSON.stringify(a));//'[{"key":"1","value":"3333"}]');
	}
	function onClose(evt) {
		writeToScreen("DISCONNECTED");
	}

	function onMessage(evt) {
		console.log(evt.data);
		debugger;
		writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data
				+ '</span>');
		//websocket.close();
	}

	function onError(evt) {
		writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
	}

	function doSend(message) {
		writeToScreen("SENT: " + message);
		websocket.send(message);
	}

	function writeToScreen(message) {
		var pre = document.createElement("p");
		pre.style.wordWrap = "break-word";
		pre.innerHTML = message;
		output.appendChild(pre);
	}

	function timer() {
		debugger;
		try {
			websocket.send(i);
		} catch (err) {

		}
	}

	window.addEventListener("load", init, false);

	var websocket = new WebSocket(wsUri);
	var i = 0;
</script>

<h2>WebSocket Test</h2>
<input type="button" onclick="timer()" value="send" />
<div id="output"></div>
</html>